package com.suql.networktools

import com.suql.networktools.interceptor.HeaderInterceptor
import com.suql.networktools.service.DataService
import com.suql.networktools.service.UserService
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitManage private constructor(){

    private val mRetrofit: Retrofit
    private var mUserService: UserService? = null
    private var mDataService: DataService? = null
    val userService: UserService?
        get() {
            if (mUserService == null) {
                mUserService = mRetrofit.create<UserService>(UserService::class.java)
            }
            return mUserService
        }
    val dataService: DataService?
        get() {
            if (mDataService == null) {
                mDataService = mRetrofit.create<DataService>(DataService::class.java)
            }
            return mDataService
        }

    init {
        val builder = OkHttpClient.Builder()
        builder.addInterceptor(HeaderInterceptor())
        if (NetWorkConstants.DEBUG) {
            val loggingInterceptor = HttpLoggingInterceptor()
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
            builder.addInterceptor(loggingInterceptor)
        }
        mRetrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
    }

    /**
     * 伴随对象
     */
    companion object {
        private val BASE_URL: String

        init {
            val ip = AppPreference.getString(AppPreference.IP_ADDRESS, "")
            if (NetWorkConstants.DEBUG) {
                if (!ip.isNullOrEmpty()) {
                    BASE_URL = "http://" + ip!!
                } else {
                    BASE_URL = "http://192.168.1.92:8080/MyFirstWebProject/"
                }
            } else {
                if (ip.isNullOrEmpty()) {
                    BASE_URL = "http://" + ip!!
                } else {
                    BASE_URL = "http://192.168.1.92:8080/MyFirstWebProject/"
                }
            }
        }

        val instance: RetrofitManage by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            RetrofitManage()
        }

        // 与上面效果一样，写法不同
        /*class Singleton private constructor() {

            private @Volatile var instance: Singleton? = null

            fun get(): Singleton {
                if (instance == null) {
                    synchronized(this) {
                        if (instance == null) {
                            instance = Singleton()
                        }
                    }
                }
                return instance!!
            }
        }*/

        // 懒加载（线程安全）java，双重锁
        /*
        private static volatile RetrofitManage instance;

        private RetrofitManage() {
        }

        public static RetrofitManage getInstance() {
            if (instance == null) {
                synchronized (RetrofitManage.class) {
                    if（instance==null）
                    instance = new RetrofitManage();
                }
            }
            return instance;
        }*/
    }
}